<template>
  <div>
    <!-- table -->
    <a-card :bordered="false">
      <a-row>
        <a-col :sm="12" :xs="24">
          <head-info title="$t('noticeList.mj')" :content="buyerCnt" :bordered="true"/>
        </a-col>
        <a-col :sm="12" :xs="24">
          <head-info title="$t('noticeList.sj')" :content="shopCnt" :bordered="true"/>
        </a-col>
      </a-row>
    </a-card>
    <a-card style="margin-top: 24px">
      <div class="table-operator">
        <div style="margin-bottom:15px;">
          {{ $t('noticeList.lxsx') }}:
          <a-radio-group @change="handleChangeType" defaultValue="-1">
            <a-radio-button>{{ $t('noticeList.qb') }}</a-radio-button>
            <a-radio-button value="buyer">{{ $t('noticeList.mj') }}</a-radio-button>
            <a-radio-button value="shop">{{ $t('noticeList.sj') }}</a-radio-button>
          </a-radio-group>
          <a-button style="margin-left:15px;" type="primary" icon="plus" @click="handleClickAdd">{{ $t('noticeList.xj') }}</a-button>
        </div>
      </div>
      <a-table
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :loading="dataLoading"
      >
        <template slot="operation" slot-scope="text, record">
          <span>
            <a-popconfirm title="$t('noticeList.sfysccx')" @confirm="handleClickDelete(record.id)">
              <a style="color:red">{{ $t('noticeList.sc') }}</a>
            </a-popconfirm>
          </span>
        </template>
      </a-table>
      <div style="margin-top:15px;">
        <MyPage :curr="page.curr" :size="page.size" :all="page.total" @changePage="handleChangePage" @changePageSize="handleChangePageSize"/>
      </div>
    </a-card>
    <create-form ref="createModal" @ok="handleAddOk" />
  </div>
</template>

<script>
import HeadInfo from '@/components/tools/HeadInfo'
import CreateForm from './CreateForm'
import { mixin, mixinDevice } from '@/utils/mixin'
import MyPage from '../Page'

export default {
  name: 'content_bind',
  mixins: [mixin, mixinDevice],
  components: {
    HeadInfo,
    CreateForm,
    MyPage
  },
  data () {
    return {
      description: this.$t('noticeList.bxgg'),
      loading: false,
      dataLoading: false,
      // table
      columns: [
        {
          title: this.$t('noticeList.lx'),
          dataIndex: 'belong',
          key: 'belong',
          scopedSlots: { customRender: 'belong' }
        },
        {
          title: this.$t('noticeList.nr'),
          dataIndex: 'title',
          key: 'title',
          scopedSlots: { customRender: 'title' }
        },
        {
          title: this.$t('noticeList.rq'),
          dataIndex: 'created_at',
          key: 'created_at',
          scopedSlots: { customRender: 'created_at' }
        },
        {
          title: this.$t('noticeList.cz'),
          key: 'action',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      data: [],
      errors: [],
      buyerCnt: 0,
      shopCnt: 0,
      showBindInfo: false,
      page: {
        curr: 1,
        size: 20,
        total: 0
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleClickHideBindInfo () {
      this.showBindInfo = false
    },
    handleClickAdd () {
      this.$refs.createModal.add()
    },
    getList (type) {
      let url = '/notice/list?page=' + this.page.curr + '&size=' + this.page.size
      if (type !== undefined) {
        url += '&type=' + type
      }
      this.$http.get(url).then(res => {
        this.data = res.list
        this.page.total = res.total
        this.buyerCnt = 0
        this.shopCnt = 0
        this.data.forEach(item => {
          if (item.belong === this.$t('noticeList.mj')) {
            this.buyerCnt++
          } else {
            this.shopCnt++
          }
        })
      }).catch(err => {
        this.$notification.error({
          message: this.$t('noticeList.sb'),
          description: err.message
        })
      })
    },
    handleClickDelete (id) {
      this.$http.post('/notice/delete', this.$qs.stringify({
        id: id
      })).then(res => {
        const newData = this.data.filter(item => item.id !== id)
        this.data = newData
      }).catch(err => {
        this.$notification.error({
          message: this.$t('noticeList.sb'),
          description: err.message
        })
      })
    },
    handleChangeType (e) {
      this.getList(e.target.value)
    },
    handleAddOk (form) {
      this.$http.post('/notice/add', this.$qs.stringify(form)).then(res => {
        if (res.code === 0) {
          this.getList()
          this.$notification.success({
            message: this.$t('noticeList.cg'),
            description: res.msg
          })
        } else {
          this.$notification.error({
            message: this.$t('noticeList.sb'),
            description: res.msg
          })
        }
      }).catch(err => {
        this.$notification.error({
          message: this.$t('noticeList.sb'),
          description: err.message
        })
      })
    },
    handleChangePageSize (size) {
      this.page.size = size
      this.getList()
    },
    handleChangePage (page) {
      this.page.curr = page
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
  .card{
    margin-bottom: 24px;
  }
  .douyin-bind-img{
    margin-bottom:10px;
    text-align: center;
  }
  .douyin-bind-img img{
    width:100px;
    height:100px;
    border-radius: 50%;
  }
</style>
